package com.netease.financial.data.entity.mapper;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.netease.financial.data.entity.AppUpdateEntity;

import javax.inject.Inject;

/**
 * Class used to transform from Strings representing json to valid objects.
 */
public class NfsEntityJsonMapper {

    private final Gson gson;

    @Inject
    public NfsEntityJsonMapper() {
        this.gson = new Gson();
    }

    /**
     * Transform from valid json string to {@link AppUpdateEntity}.
     *
     * @param jsonString A json representing a app update info profile.
     * @return {@link AppUpdateEntity}.
     */
    public AppUpdateEntity transformToAppUpdateEntity(String jsonString) {
        return fromJson(jsonString, AppUpdateEntity.class);
    }

    /**
     * Serialize an object to Json.
     *
     * @param entity {@link AppUpdateEntity} to serialize.
     */
    public String transformFromAppUpdateEntity(AppUpdateEntity entity) {
        return toJson(entity);
    }

    private String toJson(Object src) {
        return this.gson.toJson(src);
    }

    private <T> T fromJson(String jsonString, Class<T> classOfT) {
        T target = null;
        try {
            target = this.gson.fromJson(jsonString, classOfT);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return target;
    }

}
